//
//  RSWebView.m
//  StaffTax
//
//  Created by 侯伟 on 2020/3/13.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "RSWebView.h"
#import "RSWebView+Proxy.h"
#import "RSWebView+WKUIDelegate.h"
#import "RSWebView+WKNavigationDelegate.h"

@interface RSWebView ()

@end

@implementation RSWebView

+ (WKWebViewConfiguration*)defaultConfiguration{
    
    WKWebViewConfiguration* config = [WKWebViewConfiguration new];
    config.processPool = [WKProcessPool new];
    config.userContentController = [WKUserContentController new];
    config.allowsInlineMediaPlayback = YES;
    
    config.websiteDataStore = WKWebsiteDataStore.defaultDataStore;
    if (@available(iOS 10.0, *)) {
        config.mediaTypesRequiringUserActionForPlayback = NO;
    }else{
        config.requiresUserActionForMediaPlayback = NO;
    }
    return config;
    
}

- (instancetype)init{
    return [[RSWebView alloc] initWithFrame:CGRectZero config:RSWebView.defaultConfiguration];
}

- (instancetype)initWithFrame:(CGRect)frame{
    return [[RSWebView alloc] initWithFrame:frame config:RSWebView.defaultConfiguration];
}

- (instancetype)initWithFrame:(CGRect)frame config:(WKWebViewConfiguration*)config{
    if(self = [super initWithFrame:frame]){
        [self setupWithConfig:config];
    }
    return self;
}

- (void)setupWithConfig:(WKWebViewConfiguration*)config{
    
    self.backgroundColor = UIColor.whiteColor;
    
    MyLinearLayout* layout = [[MyLinearLayout alloc] initWithOrientation:(MyOrientation_Vert)];
    layout.myMargin = 0;
    [self addSubview:layout];
    
//    UIProgressView* progressView = [UIProgressView new];
//    progressView.tintColor = COLOR_THEME;
//    progressView.myMargin = 0;
//    progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//    [layout addSubview:progressView];
//    _progressView = progressView;
//    self.progressView.visibility = MyVisibility_Gone;
    
    WKWebView* webView = [[WKWebView alloc] initWithFrame:self.bounds configuration:config];
    webView.UIDelegate = self;
    webView.navigationDelegate = self;
    webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    webView.myMargin = 0;
    [layout addSubview:webView];
    _webView = webView;
    
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:(NSKeyValueObservingOptionNew) context:NULL];
}

#pragma mark -- setter

- (void)setProgressViewHidden:(BOOL)progressViewHidden{
    _progressViewHidden = progressViewHidden;
    
    self.progressView.progress = 0;
    if(progressViewHidden){
        self.progressView.visibility = MyVisibility_Gone;
    }else{
        self.progressView.visibility = MyVisibility_Visible;
    }
}


#pragma mark --private

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    if([keyPath isEqualToString:@"estimatedProgress"]){
        self.progressView.progress = 0.1 + self.webView.estimatedProgress*0.9;
    }
}

- (void)dealloc{
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
@end
